package com.example.exammaster.mapper;

import com.example.exammaster.pojo.Exam;
import com.example.exammaster.pojo.Topic;
import com.example.exammaster.pojo.WrongExam;
import com.example.exammaster.pojo.comment;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ExamMapper {

    // offset跳过的数据量
    @Select("select * from mathExam left join mathSelect on mathExam.projectSelectId=mathSelect.id where projectSelectId=#{projectSelectId} limit 1 offset #{examId}")
    Exam getMathExam(int examId,int projectSelectId);

    @Select("select * from politicalExam left join politicalSelect on politicalExam.projectSelectId=politicalSelect.id where projectSelectId=#{projectSelectId} limit 1 offset #{examId}")
    Exam getPoliticalExam(int examId,int projectSelectId);

    @Select("select * from majorExam left join majorSelect on majorExam.projectSelectId=majorSelect.id where projectSelectId=#{projectSelectId} limit 1 offset #{examId}")
    Exam getMajorExam(int examId,int projectSelectId);

    @Update("update exam set submit=#{submit} where id=#{examId}")
    int update(int examId,char submit);

    @Insert("insert into mathWrong(date,examId,projectSelectId) values(#{date},#{id},#{projectSelectId})")
    int addMathWrong(int projectSelectId, String date,int id);

    @Select("select * from mathWrong where examId=#{id} and date=#{date}")
    Exam selectMathWrongRepeat(int id, String date);

    @Insert("insert into politicalWrong (date,examId,projectSelectId) values(#{date},#{id},#{projectSelectId})")
    int addPoliticalWrong(int projectSelectId, String date,int id);

    @Select("select * from politicalWrong where examId=#{id} and date=#{date}")
    Exam selectPoliticalWrongRepeat(int id, String date);

    @Insert("insert into majorWrong (date,examId,projectSelectId) values(#{date},#{id},#{projectSelectId})")
    int addMajorWrong( int projectSelectId, String date,int id);

    @Select("select * from majorWrong where examId=#{id} and date=#{date}")
    Exam selectMajorWrongRepeat(int id, String date);

    @Select("select distinct date from mathWrong where projectSelectId=#{projectSelectId}")
    List<String> getMathWrongList(int projectSelectId);

    @Select("select distinct date from politicalWrong where projectSelectId=#{projectSelectId}")
    List<String> getPoliticalWrongList(int projectSelectId);

    @Select("select distinct date from majorWrong where projectSelectId=#{projectSelectId}")
    List<String> getMajorWrongList(int projectSelectId);

    @Select("select distinct date from mathWrong where date=#{date} and projectSelectId=#{projectSelectId}")
    List<String> getMathWrongListByDate(String date, int projectSelectId);

    @Select("select distinct date from politicalWrong where date=#{date} and projectSelectId=#{projectSelectId}")
    List<String> getPoliticalWrongListByDate(String date, int projectSelectId);

    @Select("select distinct date from majorWrong where date=#{date} and projectSelectId=#{projectSelectId}")
    List<String> getMajorWrongListByDate(String date, int projectSelectId);

    @Select("select * from mathWrong LEFT JOIN mathExam on mathWrong.examId=mathExam.id left join mathChapter on mathExam.chapterId=mathChapter.id where date=#{date} and mathExam.projectSelectId=#{projectSelectId} limit 1 offset #{id}")
    Exam getMathWrongByDate(String date,int id,int projectSelectId);

    @Select("select * from politicalWrong LEFT JOIN politicalExam on politicalWrong.examId=politicalExam.id left join politicalChapter on politicalExam.chapterId=politicalChapter.id where date=#{date} and politicalExam.projectSelectId=#{projectSelectId} limit 1 offset #{id}")
    Exam getPoliticalWrongByDate(String date,int id,int projectSelectId);

    @Select("select * from majorWrong LEFT JOIN majorExam on majorWrong.examId=majorExam.id left join majorChapter on majorExam.chapterId=majorChapter.id where date=#{date} and majorExam.projectSelectId=#{projectSelectId} limit 1 offset #{id}")
    Exam getMajorWrongByDate(String date,int id,int projectSelectId);

    @Select("select name from mathSelect")
    List<String> getMathProjectSelectList();

    @Select("select name from politicalSelect")
    List<String> getPoliticalProjectSelectList();

    @Select("select name from majorSelect")
    List<String> getMajorProjectSelectList();

    @Select("select count(id) from mathExam where projectSelectId=#{projectSelectId}")
    int getMathQuestionCount(int projectSelectId);

    @Select("select count(id) from politicalExam where projectSelectId=#{projectSelectId}")
    int getPoliticalQuestionCount(int projectSelectId);

    @Select("select count(id) from majorExam where projectSelectId=#{projectSelectId}")
    int getMajorQuestionCount(int projectSelectId);

    @Select("select count(id) from mathWrong where projectSelectId=#{projectSelectId} and date = #{date}")
    int getMathWrongQuestionCount(int projectSelectId,String date);

    @Select("select count(id) from politicalWrong where projectSelectId=#{projectSelectId} and date = #{date}")
    int getPoliticalWrongQuestionCount(int projectSelectId,String date);

    @Select("select count(id) from majorWrong where projectSelectId=#{projectSelectId} and date = #{date}")
    int getMajorWrongQuestionCount(int projectSelectId,String date);

    @Insert("insert into mathWrongRate(projectSelectId,wrongRate,date) values(#{projectSelectId},#{wrongRate},#{date})")
    int setMathWrongRate(int projectSelectId,String wrongRate,String date);

    @Select("select count(id) from mathWrongRate where projectSelectId=#{projectSelectId} and date=#{date}")
    int selectMathWrongRateRepeat(int projectSelectId, String date);

    @Insert("insert into politicalWrongRate(projectSelectId,wrongRate,date) values(#{projectSelectId},#{wrongRate},#{date})")
    int setPoliticalWrongRate(int projectSelectId,String wrongRate,String date);

    @Select("select count(id) from politicalWrongRate where projectSelectId=#{projectSelectId} and date=#{date}")
    int selectPoliticalWrongRateRepeat(int projectSelectId, String date);

    @Insert("insert into majorWrongRate(projectSelectId,wrongRate,date) values(#{projectSelectId},#{wrongRate},#{date})")
    int setMajorWrongRate(int projectSelectId,String wrongRate,String date);

    @Select("select count(id) from majorWrongRate where projectSelectId=#{projectSelectId} and date=#{date}")
    int selectMajorWrongRateRepeat(int projectSelectId, String date);

    @Update("update mathWrongRate set wrongRate=#{wrongRate} where projectSelectId=#{projectSelectId} and date=#{date}")
    int updateMathWrongRate(int projectSelectId,String wrongRate,String date);

    @Update("update politicalWrongRate set wrongRate=#{wrongRate} where projectSelectId=#{projectSelectId} and date=#{date}")
    int updatePoliticalWrongRate(int projectSelectId,String wrongRate,String date);

    @Update("update majorWrongRate set wrongRate=#{wrongRate} where projectSelectId=#{projectSelectId} and date=#{date}")
    int updateMajorWrongRate(int projectSelectId,String wrongRate,String date);

    @Insert("insert into mathQuestionSolved(projectSelectId,count,date,wrongCount) values(#{projectSelectId},1,#{date},0)")
    int insertMathQuestionSolvedCount(int projectSelectId,String date);

    @Select("select count(id) from mathQuestionSolved where projectSelectId=#{projectSelectId} and date=#{date}")
    int selectMathQuestionCountSolvedRepeat(int projectSelectId,String date);

    @Update("update mathQuestionSolved set count=#{count} where projectSelectId=#{projectSelectId} and date=#{date}")
    int updateMathQuestionSolvedCount(int projectSelectId,String date,int count);

    @Select("select count from mathQuestionSolved where projectSelectId=#{projectSelectId} and date=#{date}")
    int getMathQuestionSolvedCount(int projectSelectId,String date);

    @Select("select wrongCount from mathQuestionSolved where projectSelectId=#{projectSelectId} and date=#{date}")
    int getMathQuestionSolvedWrongCount(int projectSelectId,String date);

    @Insert("insert into politicalQuestionSolved(projectSelectId,count,date) values(#{projectSelectId},0,#{date})")
    int insertPoliticalQuestionSolvedCount(int projectSelectId,String date);

    @Select("select count(id) from politicalQuestionSolved where projectSelectId=#{projectSelectId} and date=#{date}")
    int selectPoliticalQuestionCountSolvedRepeat(int projectSelectId,String date);

    @Update("update politicalQuestionSolved set count=#{count} where projectSelectId=#{projectSelectId} and date=#{date}")
    int updatePoliticalQuestionSolvedCount(int projectSelectId,String date,int count);

    @Select("select count from politicalQuestionSolved where projectSelectId=#{projectSelectId} and date=#{date}")
    int getPoliticalQuestionSolvedCount(int projectSelectId,String date);

    @Select("select wrongCount from politicalQuestionSolved where projectSelectId=#{projectSelectId} and date=#{date}")
    int getPoliticalQuestionSolvedWrongCount(int projectSelectId,String date);

    @Insert("insert into majorQuestionSolved(projectSelectId,count,date) values(#{projectSelectId},0,#{date})")
    int insertMajorQuestionSolvedCount(int projectSelectId,String date);

    @Select("select count(id) from majorQuestionSolved where projectSelectId=#{projectSelectId} and date=#{date}")
    int selectMajorQuestionCountSolvedRepeat(int projectSelectId,String date);

    @Update("update majorQuestionSolved set count=#{count} where projectSelectId=#{projectSelectId} and date=#{date}")
    int updateMajorQuestionSolvedCount(int projectSelectId,String date,int count);

    @Select("select count from majorQuestionSolved where projectSelectId=#{projectSelectId} and date=#{date}")
    int getMajorQuestionSolvedCount(int projectSelectId,String date);

    @Select("select wrongCount from majorQuestionSolved where projectSelectId=#{projectSelectId} and date=#{date}")
    int getMajorQuestionSolvedWrongCount(int projectSelectId,String date);

    @Update("update mathQuestionSolved set wrongCount=#{wrongCount} where projectSelectId=#{projectSelectId} and date=#{date}")
    int updateMathQuestionSolvedWrongCount(int projectSelectId,String date,int wrongCount);

    @Update("update politicalQuestionSolved set wrongCount=#{wrongCount} where projectSelectId=#{projectSelectId} and date=#{date}")
    int updatePoliticalQuestionSolvedWrongCount(int projectSelectId,String date,int wrongCount);

    @Update("update majorQuestionSolved set wrongCount=#{wrongCount} where projectSelectId=#{projectSelectId} and date=#{date}")
    int updateMajorQuestionSolvedWrongCount(int projectSelectId,String date,int wrongCount);

    @Select("select wrongRate from mathWrongRate where projectSelectId=#{projectSelectId} and date=#{date}")
    String getMathQuestionSolvedRate(int projectSelectId,String date);

    @Select("select wrongRate from politicalWrongRate where projectSelectId=#{projectSelectId} and date=#{date}")
    String getPoliticalQuestionSolvedRate(int projectSelectId,String date);

    @Select("select wrongRate from majorWrongRate where projectSelectId=#{projectSelectId} and date=#{date}")
    String getMajorQuestionSolvedRate(int projectSelectId,String date);

    @Select("select wrongRate from mathWrongRate where projectSelectId=#{projectSelectId}")
    List<String> getMathLineData(int projectSelectId);

    @Select("select wrongRate from politicalWrongRate where projectSelectId=#{projectSelectId}")
    List<String> getPoliticalLineData(int projectSelectId);

    @Select("select wrongRate from majorWrongRate where projectSelectId=#{projectSelectId}")
    List<String> getMajorLineData(int projectSelectId);

    @Select("select date from mathWrongRate where projectSelectId=#{projectSelectId}")
    List<String> getMathLineDateList(int projectSelectId);

    @Select("select date from politicalWrongRate where projectSelectId=#{projectSelectId}")
    List<String> getPoliticalLineDateList(int projectSelectId);

    @Select("select date from majorWrongRate where projectSelectId=#{projectSelectId}")
    List<String> getMajorLineDateList(int projectSelectId);

    @Select("select name from mathSelect where id=#{projectSelectId}")
    String getMathLineName(int projectSelectId);

    @Select("select name from politicalSelect where id=#{projectSelectId}")
    String getPoliticalLineName(int projectSelectId);

    @Select("select name from majorSelect where id=#{projectSelectId}")
    String getMajorLineName(int projectSelectId);





    @Select("select count from mathChapter where id=#{chapterId}")
    int selectMathChapterCount(int chapterId);

    @Select("update mathChapter set count=#{count} where id=#{chapterId}")
    void updateMathChapterCount(int chapterId,int count);

    @Select("select count from politicalChapter where id=#{chapterId}")
    int selectPoliticalChapterCount(int chapterId);

    @Select("update politicalChapter set count=#{count} where id=#{chapterId}")
    void updatePoliticalChapterCount(int chapterId,int count);

    @Select("select count from majorChapter where id=#{chapterId}")
    int selectMajorChapterCount(int chapterId);

    @Select("update majorChapter set count=#{count} where id=#{chapterId}")
    void updateMajorChapterCount(int chapterId,int count);

    //----------------------------

    @Select("select wrongCount from mathChapter where id=#{chapterId}")
    int selectMathChapterWrongCount(int chapterId);

    @Select("update mathChapter set wrongCount=#{count} where id=#{chapterId}")
    void updateMathChapterWrongCount(int chapterId,int count);

    @Select("select wrongCount from politicalChapter where id=#{chapterId}")
    int selectPoliticalChapterWrongCount(int chapterId);

    @Select("update politicalChapter set wrongCount=#{count} where id=#{chapterId}")
    void updatePoliticalChapterWrongCount(int chapterId,int count);

    @Select("select wrongCount from majorChapter where id=#{chapterId}")
    int selectMajorChapterWrongCount(int chapterId);

    @Select("update majorChapter set wrongCount=#{count} where id=#{chapterId}")
    void updateMajorChapterWrongCount(int chapterId,int count);

    @Select("select chapter from mathChapter where projectSelectId=#{projectSelectId}")
    List<String> selectMathChapterList(int projectSelectId);

    @Select("select chapter from politicalChapter where projectSelectId=#{projectSelectId}")
    List<String> selectPoliticalChapterList(int projectSelectId);

    @Select("select chapter from majorChapter where projectSelectId=#{projectSelectId}")
    List<String> selectMajorChapterList(int projectSelectId);

    @Update("update mathChapter set rate=#{rate} where id=#{chapterId}")
    void calMathChapterRate(int chapterId,double rate);

    @Update("update politicalChapter set rate=#{rate} where id=#{chapterId}")
    void calPoliticalChapterRate(int chapterId,double rate);

    @Update("update majorChapter set rate=#{rate} where id=#{chapterId}")
    void calMajorChapterRate(int chapterId,double rate);

    @Select("select rate from mathChapter where projectSelectId=#{projectSelectId}")
    double[] getMathChapterRate(int projectSelectId);

    @Select("select rate from politicalChapter where projectSelectId=#{projectSelectId}")
    double[] getPoliticalChapterRate(int projectSelectId);

    @Select("select rate from majorChapter where projectSelectId=#{projectSelectId}")
    double[] getMajorChapterRate(int projectSelectId);

    @Select("select * from mathExam where projectSelectId=#{projectSelectId} and chapterId=#{chapterId} limit 1 offset #{questionId}")
    Exam getMathChapterQuestion(int projectSelectId,int chapterId,int questionId);

    @Select("select * from politicalExam where projectSelectId=#{projectSelectId} and chapterId=#{chapterId} limit 1 offset #{questionId}")
    Exam getPoliticalChapterQuestion(int projectSelectId,int chapterId,int questionId);

    @Select("select * from majorExam where projectSelectId=#{projectSelectId} and chapterId=#{chapterId} limit 1 offset #{questionId}")
    Exam getMajorChapterQuestion(int projectSelectId,int chapterId,int questionId);

    @Select("select count(id) from mathExam where projectSelectId=#{projectSelectId} and chapterId=#{chapterId}")
    int getMathChapterCount(int projectSelectId,int chapterId);

    @Select("select count(id) from politicalExam where projectSelectId=#{projectSelectId} and chapterId=#{chapterId}")
    int getPoliticalChapterCount(int projectSelectId,int chapterId);

    @Select("select count(id) from majorExam where projectSelectId=#{projectSelectId} and chapterId=#{chapterId}")
    int getMajorChapterCount(int projectSelectId,int chapterId);

    @Select("select chapter from mathChapter where rate<50 and projectSelectId=#{projectSelectId}")
    List<String> selectMathShortList(int projectSelectId);

    @Select("select chapter from politicalChapter where rate<50 and projectSelectId=#{projectSelectId}")
    List<String> selectPoliticalShortList(int projectSelectId);

    @Select("select chapter from majorChapter where rate<50 and projectSelectId=#{projectSelectId}")
    List<String> selectMajorShortList(int projectSelectId);

    @Select("select distinct examYear from mathExam where projectSelectId=#{projectSelectId}")
    List<String> getMathYearList(int projectSelectId);

    @Select("select distinct examYear from politicalExam where projectSelectId=#{projectSelectId}")
    List<String> getPoliticalYearList(int projectSelectId);

    @Select("select distinct examYear from majorExam where projectSelectId=#{projectSelectId}")
    List<String> getMajorYearList(int projectSelectId);

    @Select("select distinct examId from mathWrong where projectSelectId=#{projectSelectId}")
    int[] getMathExamIdList(int projectSelectId);

    @Select("select distinct examId from politicalWrong where projectSelectId=#{projectSelectId}")
    int[] getPoliticalExamIdList(int projectSelectId);

    @Select("select distinct examId from majorWrong where projectSelectId=#{projectSelectId}")
    int[] getMajorExamIdList(int projectSelectId);

    @Select("select chapterId from mathExam where id=#{examId}")
    int getMathChapterId(int examId);

    @Select("select chapterId from politicalExam where id=#{examId}")
    int getPoliticalChapterId(int examId);

    @Select("select chapterId from majorExam where id=#{examId}")
    int getMajorChapterId(int examId);

    @Select("select count(id) from mathExam where projectSelectId=#{projectSelectId} and chapterId=#{chapterId}")
    int getMathCountByChapterId(int projectSelectId,int chapterId);

    @Select("select count(id) from politicalExam where projectSelectId=#{projectSelectId} and chapterId=#{chapterId}")
    int getPoliticalCountByChapterId(int projectSelectId,int chapterId);

    @Select("select count(id) from majorExam where projectSelectId=#{projectSelectId} and chapterId=#{chapterId}")
    int getMajorCountByChapterId(int projectSelectId,int chapterId);

    @Select("select * from topicList")
    List<Topic> getTopicList();

    @Insert("insert into topicList(img,title,date,submitImg) values(#{img},#{title},#{date},#{submitImg})")
    void addTopic(String img,String title,String date,String submitImg);

    @Select("select count(DISTINCT date) from mathWrongRate where projectSelectId=#{projectSelectId}")
    int getMathStudyDay(int projectSelectId);

    @Select("select count(DISTINCT date) from politicalWrongRate where projectSelectId=#{projectSelectId}")
    int getPoliticalStudyDay(int projectSelectId);

    @Select("select count(DISTINCT date) from majorWrongRate where projectSelectId=#{projectSelectId}")
    int getMajorStudyDay(int projectSelectId);

    @Select("select * from topicList where id=#{id}")
    Topic getTopic(int id);

    @Insert("insert into comment(topicId,img,comment,date) values(#{topicId},#{img},#{comment},#{date})")
    void addComment(int topicId,String img,String comment,String date);

    @Select("select * from comment where topicId=#{topicId}")
    List<comment> getComment(int topicId);

    @Insert("insert into mathExam(title,answerA,answerB,answerC,answerD,realAnswer,projectSelectId,chapterId,examYear,isImage) values(#{title},#{answerA},#{answerB},#{answerC},#{answerD},#{realAnswer},#{projectSelectId},#{chapterId},#{examYear},#{isImage})")
    void addMathQuestion(String title,String answerA,String answerB,String answerC,String answerD,String realAnswer,int projectSelectId,int chapterId,String examYear,String isImage);

    @Select("select * from mathExam left join mathSelect on mathExam.projectSelectId = mathSelect.id")
    List<Exam> getMathQuestion();

    @Select("select * from mathExam left join mathSelect on mathExam.projectSelectId=mathSelect.id where projectSelectId=#{projectSelectId} and chapterId=#{chapterId}")
    List<Exam> selectMathQuestion(int projectSelectId,int chapterId);

    @Select("select * from mathExam left join mathSelect on mathExam.projectSelectId=mathSelect.id where mathExam.id=#{examId}")
    Exam getMathQuestionById(int examId);

    @Update("update mathExam set title=#{title},answerA=#{answerA},answerB=#{answerB},answerC=#{answerC},answerD=#{answerD},realAnswer=#{realAnswer},projectSelectId=#{projectSelectId},examYear=#{examYear},isImage=#{isImage} where id=#{examId}")
    void updateMathQuestionById(String title,String answerA,String answerB,String answerC,String answerD,String realAnswer,int projectSelectId,String examYear,String isImage,int examId);

    @Select("select * from politicalExam left join politicalSelect on politicalExam.projectSelectId = politicalSelect.id")
    List<Exam> getPoliticalQuestion();

    @Select("select * from majorExam left join majorSelect on majorExam.projectSelectId = majorSelect.id")
    List<Exam> getMajorQuestion();

    @Select("select * from mathWrong left join mathSelect on mathWrong.projectSelectId=mathSelect.id")
    List<WrongExam> getMathWrongQuestion();

    @Select("select * from politicalWrong left join politicalSelect on politicalWrong.projectSelectId=politicalSelect.id")
    List<WrongExam> getPoliticalWrongQuestion();

    @Select("select * from majorWrong left join majorSelect on majorWrong.projectSelectId=majorSelect.id")
    List<WrongExam> getMajorWrongQuestion();

    @Select("select * from comment")
    List<comment> getCommentList();
}
